﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.ViewQuery;

public partial class Theme_Index_xkzx : PowerCreator.LMS.Web.Base.WebBasePage
{
   protected  Course opCourse = new Course();
    Catalog opCatalog = new Catalog();
    ViewQuery opViewQuery = new ViewQuery();
    CatalogCourse opCatalogCourse = new CatalogCourse();
    PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();
    PowerCreator.LMS.Web.DL.Project.TOSchoolStage opSchoolStage = new PowerCreator.LMS.Web.DL.Project.TOSchoolStage();
    PowerCreator.LMS.Web.Course.UserCourse opUserCourse = new PowerCreator.LMS.Web.Course.UserCourse();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";


    private string _SQL_Catalog_Course = "select "
        + " tcc.CatalogcouID,tg.CatalogId,tg.CatalogName,tc.Course_Id,tc.CourseName,tc.CourseMostlyTeacherName,tc.CourseIsCompulsory,tc.Description,tc.CourseState ,tc.CourseResourceNum"
        + " from TCatalogCourse tcc left join TCatalog tg on tcc.FKCatalogcouCatalogID=tg.CatalogId "
        + " left join TO_Course tc on tcc.FKCatalogcouCourseID=tc.Course_Id ";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "课程菜单";
        CheckUser();
        if (!IsPostBack)
        {
        //    //btnDel.Visible = IsLicensed("DelCourse");
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            ViewState["OrderList"] = "";//排序
            ViewState["OrderType"] = " desc ";//排序方式
        //    InitPage();
            BinderCatalog();
            BinderXueduan();
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    protected override void InitPage()
    {
        base.InitPage();
    }


    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }
        //else
        //{
        //    验证此课件该用户是否可以点播
        //    PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[WebConfig.SESSION_USER_USERINFO];
        //    string courseIDlist = getMyCourseIDlist();
        //}
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(_SQL_Catalog_Course + " where 1=1 and tc.Used=1 and tc.CourseIsCompulsory=2");

        if (ddl_Catalog.SelectedValue!="0")
        {
            sbSearch.Append(string.Format(" and tg.CatalogId={0} ", ddl_Catalog.SelectedValue));
        }

        if (ddl_xueduan.SelectedValue != "0")
        {
            sbSearch.Append(string.Format(" and tc.Xueduan={0} ", ddl_xueduan.SelectedValue));
        }


        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
        {
            sbSearch.Append(string.Format(" and tc.CourseName like '%{0}%' and tc.CourseMostlyTeacherName like '%{0}%' ", txt_KeyWord.Text));
        }
        return sbSearch.ToString();

    }
    protected void btn_seach_Click(object sender, EventArgs e)
    {
        ////ViewState["DomainID"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    private void BinderCatalog()
    {
        string search = "";
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = opCatalog.GetCatalogTree(dt);
        //opCatalog.GetCatalogTreeView(dt,ref tv_Catalog);

        ddl_Catalog.DataSource = list;
        ddl_Catalog.DataTextField = "Text";
        ddl_Catalog.DataValueField = "Value";
        ddl_Catalog.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部学科";
        li.Value = "0";
        ddl_Catalog.Items.Insert(0, li);
    }
    private void BinderXueduan()
    {
        string search = "";
        DataTable dt = opSchoolStage.Query(search);

        ddl_xueduan.DataSource = dt;
        ddl_xueduan.DataTextField = "SchoolStage";
        ddl_xueduan.DataValueField = "ID";
        ddl_xueduan.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部学段";
        li.Value = "0";
        ddl_xueduan.Items.Insert(0, li);

    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = 12;
        AspNetPager1.RecordCount = opViewQuery.Count(search);
    }
    private void BinderRPT(string search, int pageIndex)
    {
        string orderList = ViewState["OrderList"].ToString();
        if (!string.IsNullOrEmpty(orderList))
            search += " order by " + orderList + ViewState["OrderType"].ToString();
        DataTable dt = opViewQuery.Query(search, 12, pageIndex);
        //dt.Columns.Add("bg");
        //int iResult = 0;
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    int k = Math.DivRem(i, 2, out iResult);
        //    if (iResult == 0)
        //        dt.Rows[i]["bg"] = "#FFFFFF";
        //    if (iResult == 1)
        //        dt.Rows[i]["bg"] = "#EEEEEE";
        //}
        rpt_Course.DataSource = dt;
        rpt_Course.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    //protected void btn_Search_Click(object sender, EventArgs e)
    //{
    //    //string search = GetSearch();
    //    //BinderPager(search);
    //    //BinderRPT(search, 1);
    //}
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }  

    //protected void btn_AddFKCourse_Click(object sender, EventArgs e)
    //{
    //    string url = "CourseSelect.aspx?CatalogID=" + ddl_Catalog.SelectedValue.ToString();
    //    Script.RedirectURL(url);
    //}
    //protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    if (e.CommandName.ToLower() == "leditor")
    //    {
    //        string url = "EditCourseInfo.aspx?CourseID=" + e.CommandArgument.ToString();
    //        Script.RedirectURL(url);
    //    }

    //}
    //protected void btn_SortSearch_Click(object sender, EventArgs e)
    //{
    //    ViewState["SearchType"] = "0";
    //    string search = GetSearch();
    //    BinderPager(search);
    //    BinderRPT(search, AspNetPager1.CurrentPageIndex);
    //}

    protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "sel")
        {
            string search = string.Format(" UCUserID={0} and FKUCCourseID = {1} ", login.UserID, e.CommandArgument.ToString());
            if (opUserCourse.QueryUserCourse(search) == 0)
            {
                PowerCreator.LMS.Course.Entity.UserCourse voUserCourse = new PowerCreator.LMS.Course.Entity.UserCourse();
                voUserCourse.FKUCCourseID = int.Parse(e.CommandArgument.ToString());
                voUserCourse.UCState = 1;
                voUserCourse.UCUserID = login.UserID;
                opUserCourse.AddUserCourse(voUserCourse);

                Script.Alert("选课成功，请等待管理员审核！");
            }
        }
    }
}
